/*
 * Copyright (C) 2009 Luc Boudreau
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */
package com.encon.projecttracker.server.services.impl;

import java.sql.Driver;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.springframework.util.Assert;

import com.encon.projecttracker.server.services.DiscoveryService;
import com.encon.projecttracker.server.services.QueryService;
import com.encon.projecttracker.server.services.SessionService;
import com.encon.projecttracker.server.util.JdbcDriverFinder;

/**
 * Simple service implementation as a Spring bean.
 * @author Luc Boudreau
 */
public class DiscoveryServiceImpl extends AbstractService
        implements DiscoveryService
{

        private SessionService sessionService = null;

        private QueryService queryService = null;

        private JdbcDriverFinder driverFinder = null;



        public void setSessionService(SessionService sessionService) {
                this.sessionService = sessionService;
        }



        public void setQueryService(QueryService queryService) {
        this.queryService = queryService;
    }



    public void afterPropertiesSet() throws Exception {
                Assert.notNull(sessionService);
                Assert.notNull(queryService);
                Assert.notNull(driverFinder);
        }


        public List<String> getDrivers()
        {
                this.driverFinder.registerDrivers();

                // An enumeration is a very unpractical thing, so let's convert it to a List.
                // We can't even know it's size... what a shameful object.
                Enumeration<Driver> driversEnum = DriverManager.getDrivers();
                List<String> drivers = new ArrayList<String>();
                while (driversEnum.hasMoreElements())
                        { drivers.add(driversEnum.nextElement().getClass().getName()); }

                return drivers;
        }


    


    public void setDriverFinder(JdbcDriverFinder driverFinder) {
        this.driverFinder = driverFinder;
    }
}
